﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace CNDPKlient
{
    public partial class FormMain : Form
    {
        public string ip = "";
        public int port = 0;

        public UdpClient client = new UdpClient();

        public int NumberOfThreads = 0;
        public FormMain()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
            ip = "";
            port = 0;
            labelExtra.Hide();
            pictureBoxButtons.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBoxButtons.Image = CNDPKlient.Properties.Resources.GreyButton;

            ToolTip tooltip1 = new ToolTip();
            tooltip1.AutoPopDelay = 5000;
            tooltip1.InitialDelay = 1000;
            tooltip1.ReshowDelay = 500;
            tooltip1.ShowAlways = true;

            tooltip1.SetToolTip(this.pictureBoxButtons, "Status på anslutningen");
            tooltip1.SetToolTip(this.buttonSendPoints, "Skicka in poäng");

        }

        private void anslutTillServerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var input = new FormInput();
            input.ShowDialog();
            if (input.DialogResult == DialogResult.OK)
            {
                ip = input.ipAddress;
                port = input.portNumber;
                input.Dispose();
                pictureBoxButtons.Image = CNDPKlient.Properties.Resources.GreenButton1;
            }
        }

        private void buttonSendPoints_Click(object sender, EventArgs e)
        {
            bool PointsSent = false;
            if (ip != "" && port != 0)
            {
                if (!PointsSent)
                {
                    if (comboBoxJudgePoints.SelectedIndex > -1)
                    {
                        var ep = new IPEndPoint(IPAddress.Parse(ip), port);
                        byte[] sendBytes = Encoding.ASCII.GetBytes(Convert.ToString(textBoxPersonalCodeNumber.Text + "|Points@truekil|" + comboBoxJudgePoints.SelectedItem.ToString()));
                        client.Connect(ep);
                        client.Send(sendBytes, sendBytes.Length);
                        labelExtra.Show();
                        labelExtra.Text = "Poängen har skickats in.";
                    }
                    else
                    {
                        labelExtra.Show();
                        labelExtra.Text = "Välj ett giltigt poäng.";
                    }
                }
            }
            else
            {
                labelExtra.Show();
                labelExtra.Text = "Välj ip-address och port.";
            }
        }
    }
}
